package com.example.kingdeescan.ui.recored;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.kingdeescan.R;
import com.example.kingdeescan.R2;
import com.example.kingdeescan.dao.FBiller;
import com.example.kingdeescan.dao.FDept;
import com.example.kingdeescan.dao.FEmp;
import com.example.kingdeescan.dao.FFManger;
import com.example.kingdeescan.dao.FOrg;
import com.example.kingdeescan.dao.SEOutStock;
import com.example.kingdeescan.dto.DocInfoDTO;
import com.example.kingdeescan.enums.DocType;
import com.example.kingdeescan.enums.SearchType;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import lombok.SneakyThrows;

import static com.example.kingdeescan.MainActivity.dbUtil;
import static com.example.kingdeescan.MainActivity.docInfoDTO;
import static com.example.kingdeescan.MainActivity.fragmentService;

/**
 * @author  : 2020/7/24
 * @date    : zly
 * @desc    : 必录项Fragment
 */
public class RecoredFragment extends Fragment implements View.OnClickListener {

    public static final String TAG="RecoredFragment";

    private RecoredViewModel recoredViewModel;

    @BindView(R2.id.dtype_selected_text)
    TextView dtype_selected;//单据类型文本框
    @BindView(R2.id.recored_fBillno_txt)
    TextView recored_fBillno_txt;//单据编号文本框
    @BindView(R.id.recored_forg_editText)
    EditText recored_forg_editText;//购货单位编辑框
    @BindView(R2.id.recored_fSelBillNo_editText)
    EditText recored_fSelBillNo_editText;//选单号编辑框
    @BindView(R2.id.recored_fDept_editText)
    EditText recored_fDept_editText;//部门编辑框
    @BindView(R2.id.recored_fEmp_editText)
    EditText recored_fEmp_editText;//业务员编辑框
    @BindView(R2.id.recored_fFManager_editText)
    EditText recored_fFManager_editText;//发货员编辑框
    @BindView(R2.id.recored_fBiller_editText)
    EditText recored_fBiller_editText;//制单员编辑框
    @BindView(R2.id.btn_recored_commit)
    Button btn_recored_commit;//确认按钮
    @BindView(R.id.ll_recored_parent)
    LinearLayout ll_recored_parent;//必录项的父布局

    private Bundle sendBundle;//传递搜索类型
    public int searchType;//存储当前搜索类型

    Unbinder unbinder;//绑定ButterKnife视图

    FEmp fEmp;
    FDept fDept;
    FFManger ffManger;
    FOrg fOrg;
    FBiller fBiller;
    List<SEOutStock> seOutStockList;//存储选单号信息
    String fBillNoMax;//存储最大单据编号

    int validCode =1;//存储校验状态码(默认为1:所有数据有效正确，其它:无效，对应栏位的代码)
    boolean validFlag =false;

    /**
     * 设置RecoredFragment相关接口
     * 声明了一个内部接口，来回调其点击事件，想要管理其点击事件的Activity实现此接口就即可
     */
    public interface FRecoredListener {
        void onFRecoredCommitClick(View v,Bundle recoredStatus);//确认按钮点击回调
        void onFBillNoCreatedFailEvent(View v);//单据编号生成失败事件回调
        void onFRecoredEditTextClick(View v, Bundle searchType);//编辑框点击回调
    }

    /**
     * onCreateView
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        //加载当前fragment视图
        View root = inflater.inflate(R.layout.fragment_recored, container, false);
        //返回一个Unbinder值（进行解绑），注意这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, root);

        //获取当前Fragment的ViewModel
        recoredViewModel = ViewModelProviders.of(getActivity()).get(RecoredViewModel.class);

        //判断是否选择单据类型，若为空则返回
        boolean invalidDocTypeResult = fragmentService.validDocType(getActivity(), container, this, docInfoDTO.getDocType());
        if (!invalidDocTypeResult) {
            return null;
        }
        initComponet(container);//初始化组件资源
        return root;
    }

    /**
     * 初始化组件
     */
    @SneakyThrows
    private void initComponet(ViewGroup container) {
//        connSqlByThread();//初始化时连接数据库，避免重复连接(使用多线程处理，避免堵塞主线程)
        DocType docType = docInfoDTO.getDocType();
        //显示单据类型
        if (null != docType) {
            dtype_selected.setText(docType.toString());
        }
        //生成单据编号
        setFBillNo(container,docType);
        //设置点击监听事件
        setOnClickListener();
        //设置EditText可以点击但是不可以输入
        setEditTextFocusableInvalid();
        //设置观察者对象，当有设置缓存时，才可以用观察者模式监视并更新，否则忽略
        setObserveOnViewModel();
    }

    /**
     * 设置点击监听事件
     */
    private void setOnClickListener(){
        recored_forg_editText.setOnClickListener(this);
        recored_fSelBillNo_editText.setOnClickListener(this);
        recored_fDept_editText.setOnClickListener(this);
        recored_fEmp_editText.setOnClickListener(this);
        recored_fFManager_editText.setOnClickListener(this);
        recored_fBiller_editText.setOnClickListener(this);
        btn_recored_commit.setOnClickListener(this);
    }

    /**
     * 设置EditText可以点击但是不可以输入
     */
    private void setEditTextFocusableInvalid(){
        recored_forg_editText.setCursorVisible(false);
        recored_forg_editText.setFocusable(false);
        recored_forg_editText.setFocusableInTouchMode(false);

        recored_fSelBillNo_editText.setCursorVisible(false);
        recored_fSelBillNo_editText.setFocusable(false);
        recored_fSelBillNo_editText.setFocusableInTouchMode(false);

        recored_fDept_editText.setCursorVisible(false);
        recored_fDept_editText.setFocusable(false);
        recored_fDept_editText.setFocusableInTouchMode(false);

        recored_fEmp_editText.setCursorVisible(false);
        recored_fEmp_editText.setFocusable(false);
        recored_fEmp_editText.setFocusableInTouchMode(false);

        recored_fFManager_editText.setCursorVisible(false);
        recored_fFManager_editText.setFocusable(false);
        recored_fFManager_editText.setFocusableInTouchMode(false);

        recored_fBiller_editText.setCursorVisible(false);
        recored_fBiller_editText.setFocusable(false);
        recored_fBiller_editText.setFocusableInTouchMode(false);
    }

    /**
     * 设置观察者对象，当有设置缓存时，才可以用观察者模式监视并更新，否则忽略
     */
    private void setObserveOnViewModel(){
        recoredViewModel.getfSupplyText().observe(this, s -> recored_forg_editText.setText(s));
        recoredViewModel.getfSelBillNoText().observe(this, s -> recored_fSelBillNo_editText.setText(s));
        recoredViewModel.getfDeptText().observe(this, s -> recored_fDept_editText.setText(s));
        recoredViewModel.getfEmpText().observe(this, s -> recored_fEmp_editText.setText(s));
        recoredViewModel.getfFManagerText().observe(this, s -> recored_fFManager_editText.setText(s));
        recoredViewModel.getfBillerText().observe(this, s -> recored_fBiller_editText.setText(s));

        recoredViewModel.getfOrgMLD().observe(this, fOrgBuffer -> fOrg=fOrgBuffer);
        recoredViewModel.getfDeptMLD().observe(this, fDeptBuffer -> fDept=fDeptBuffer);
        recoredViewModel.getfEmpMLD().observe(this, fEmpBuffer -> fEmp=fEmpBuffer);
        recoredViewModel.getFfMangerMLD().observe(this, ffMangerBuffer -> ffManger=ffMangerBuffer);
        recoredViewModel.getFBillerMLD().observe(this, fBillerBuffer -> fBiller=fBillerBuffer);
        recoredViewModel.getSeOutStockListMLD().observe(this, seOutStockListBuffer -> seOutStockList=seOutStockListBuffer);

        recoredViewModel.getSearchType().observe(this, integer -> {
            //设置显示查询结果值到对应控件上
            boolean searchResult = getBundle(integer);
            Log.d(TAG, "searchResult: "+searchResult);
        });
    }

    /**
     * 自动生成单据编号
     * 销售出库单据编号的规则："XOUT"+"年份/"+"月份/"+"日"+"自增号" ，如：XOUT20/08/03008
     *      注意：
     *          1.XOUT为销售出库统一前缀字符
     *          2.年月日统一采用2位表示
     *          3.自增号统一采用4位表示，从000开始
     *          4.若系统中没有当天日期对应的单据信息，则采用初始默认编号：如：XOUT20/08/03000
     */
    @SneakyThrows
    private String builderFBillNo(DocType docType){
        StringBuilder fBillNo=new StringBuilder();//单据编号
        switch (docType){
            case DOC_TYPE_SSCK:
                //根据当前日期生成模糊查询条件
                String currentDate = new SimpleDateFormat("yy/MM/dd").format(new Date());
                //查询系统销售出库的最大单据编号fBillNoMax
                Thread queryThread=new Thread(() -> {
                    try {
                        fBillNoMax = dbUtil.queryMaxFBillNo(docType, currentDate);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                queryThread.start();
                queryThread.join();
                //判断FBillNo,若为空则采用初始默认编号；否则在查询返回的FBillNoMax的自增号+1
                if (TextUtils.isEmpty(fBillNoMax)){
                    fBillNo.append("XOUT");
                    fBillNo.append(currentDate);
                    fBillNo.append("000");
                }else{
                    String billNoOld = TextUtils.substring(fBillNoMax, 12, fBillNoMax.length());//获取自增号
                    String billNoNew = new DecimalFormat("000").format(Integer.parseInt(billNoOld) + 1);
                    fBillNo.append("XOUT");
                    fBillNo.append(currentDate);
                    fBillNo.append(billNoNew);
                }
                break;
        }
        return fBillNo.toString();
    }

    /**
     * 设置自动生成的单据编号
     * @param container
     * @param docType
     */
    private void setFBillNo(ViewGroup container,DocType docType){
        //生成单据编号
        String fBillNo = builderFBillNo(docType);
        if (null!=fBillNo && fBillNo!=""){
            recored_fBillno_txt.setText(fBillNo);
        }else{
            Toast.makeText(getContext(), "单据编号生成失败!", Toast.LENGTH_SHORT).show();
            if (getActivity() instanceof FRecoredListener)
            {
                ((FRecoredListener) getActivity()).onFBillNoCreatedFailEvent(container);
            }
        }
    }

    /**
     * 进行数据校验(非空判断、数据有效性判定)
     */
    private int ValidRecoredData() {
        //首先判断数据是否为空
        if (validChildViewsValueIsEmpty()){
            return 0;
        }
//        //当数据不为空，校验数据有效性
//        validChildViewsValueIsEffective();
//        return this.validCode;
        return 1;
    }

    /**
     * 遍历所有子视图控件判断是否存在空值
     * @return
     */
    private boolean validChildViewsValueIsEmpty() {
        if (TextUtils.isEmpty(recored_forg_editText.getText())){
            return true;
        }
        if (TextUtils.isEmpty(recored_fSelBillNo_editText.getText())){
            return true;
        }
        if (TextUtils.isEmpty(recored_fDept_editText.getText())){
            return true;
        }
        if (TextUtils.isEmpty(recored_fEmp_editText.getText())){
            return true;
        }
        if (TextUtils.isEmpty(recored_fFManager_editText.getText())){
            return true;
        }
        if (TextUtils.isEmpty(recored_fBiller_editText.getText())){
            return true;
        }

        return false;
    }

    /**
     * 遍历所有子视图控件判断是否存在空值
     * @param viewGroup
     * @return
     *      false:存在空值
     *      true:无空值
     */
    private boolean validChildViewsValueIsEmpty(ViewGroup viewGroup) {
        if (viewGroup == null) {
            return false;
        }
        boolean isNonValueFlag=false;//默认为false,不存在空值
        View view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            view = viewGroup.getChildAt(i);
            if (view instanceof EditText) {
                EditText editText = (EditText) view;
                if (TextUtils.isEmpty(editText.getText())){
                    isNonValueFlag=true;
                    break;
                }
            } else if (view instanceof ViewGroup) {
                // 若是布局控件（LinearLayout或RelativeLayout）,继续查询子View
                this.validChildViewsValueIsEmpty((ViewGroup) view);
            }
        }
        return isNonValueFlag;
    }

    /**
     * 1.校验购货单位
     * 2.校验选单号
     * 3.校验部门
     * 4.校验业务员
     * 5.校验发货员
     * 6.校验制单员
     * @return
     */
    private boolean validChildViewsValueIsEffective(){
        //采用多线程校验
        Thread validThread=new Thread(()->{
            String forg= recored_forg_editText.getText().toString().trim();
            String[] split = recored_fSelBillNo_editText.getText().toString().split("\r\n");
            String fdept=recored_fDept_editText.getText().toString().trim();
            String femp=recored_fEmp_editText.getText().toString().trim();
            String ffmanger=recored_fFManager_editText.getText().toString().trim();
            String fbiller=recored_fBiller_editText.getText().toString().trim();

            try {
                if (dbUtil.validFOrg(forg)){
                    if (dbUtil.validSelBillNo(split)){
                        if (dbUtil.validFDept(fdept)){
                            if (dbUtil.validFEmp(femp)){
                                if (dbUtil.validFFManger(ffmanger)){
                                    if (dbUtil.validFBiller(fbiller)){
                                        this.validFlag =true;
                                    }else{
                                        this.validCode =SearchType.SEARCH_FBILLER_TYPE;
                                    }
                                }else{
                                    this.validCode =SearchType.SEARCH_FFMANGER_TYPE;
                                }
                            }else{
                                this.validCode =SearchType.SEARCH_FEMP_TYPE;
                            }
                        }else{
                            this.validCode =SearchType.SEARCH_FDEPT_TYPE;
                        }
                    }else{
                        this.validCode =SearchType.SEARCH_FSELBILLNO_TYPE;
                    }
                }else{
                    this.validCode =SearchType.SEARCH_FORG_TYPE;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        validThread.start();
        try {
            validThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this.validFlag;
    }

    /**
     * 设置控件焦点
     */
    private void setFocusOnNonValueComponent(){
        if (TextUtils.isEmpty(recored_forg_editText.getText())){
            recored_forg_editText.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(recored_fSelBillNo_editText.getText())){
            recored_fSelBillNo_editText.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(recored_fDept_editText.getText())){
            recored_fDept_editText.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(recored_fEmp_editText.getText())){
            recored_fEmp_editText.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(recored_fFManager_editText.getText())){
            recored_fFManager_editText.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(recored_fBiller_editText.getText())){
            recored_fBiller_editText.requestFocus();
            return;
        }
    }

    /**
     * 获取Bundle传输数据并更新控件内容
     *
     * @return
     */
    private boolean getBundle(int searchType) {
        Bundle bundle = getArguments();
        if (null == bundle) {
            return false;
        }
        Serializable serializable;
        switch (searchType){
            case SearchType.SEARCH_FORG_TYPE:
                serializable = bundle.getSerializable("SearchFOrg");
                if (null!=serializable){
                    fOrg=new FOrg();
                    fOrg= (FOrg) serializable;
                    recored_forg_editText.setText(fOrg.getFName());
//                    setFocusOnNonValueComponent();
                }
                break;
            case SearchType.SEARCH_FSELBILLNO_TYPE:
                serializable = bundle.getSerializable("SelBillNoCheckedList");
                if (null!=serializable){
                    seOutStockList=new ArrayList<>();
                    seOutStockList= (List<SEOutStock>) bundle.getSerializable("SelBillNoCheckedList");
                    StringBuilder stringBuilder=new StringBuilder();
                    if (seOutStockList.size()>0){
                        for (int i = 0; i < seOutStockList.size(); i++) {
                            if (i==seOutStockList.size()-1){
                                stringBuilder.append(seOutStockList.get(i).getFBillNo());
                            }else{
                                stringBuilder.append(seOutStockList.get(i).getFBillNo());
                                stringBuilder.append("\r\n");
                            }
                        }
                    }
                    recored_fSelBillNo_editText.setText(stringBuilder);
//                    setFocusOnNonValueComponent();
                }
                break;
            case SearchType.SEARCH_FDEPT_TYPE:
                serializable = bundle.getSerializable("SearchFDept");
                if (null!=serializable){
                    fDept=new FDept();
                    fDept= (FDept) serializable;
                    recored_fDept_editText.setText(fDept.getFName());
//                    setFocusOnNonValueComponent();
                }
                break;
            case SearchType.SEARCH_FEMP_TYPE:
                serializable = bundle.getSerializable("SearchFEmp");
                if (null!=serializable){
                    fEmp=new FEmp();
                    fEmp= (FEmp) serializable;
                    recored_fEmp_editText.setText(fEmp.getFName());
//                    setFocusOnNonValueComponent();
                }
                break;
            case SearchType.SEARCH_FFMANGER_TYPE:
                serializable = bundle.getSerializable("SearchFFManger");
                if (null!=serializable){
                    ffManger=new FFManger();
                    ffManger= (FFManger) serializable;
                    recored_fFManager_editText.setText(ffManger.getFName());
//                    setFocusOnNonValueComponent();
                }
                break;
            case SearchType.SEARCH_FBILLER_TYPE:
                serializable = bundle.getSerializable("SearchFBiller");
                if (null!=serializable){
                    fBiller=new FBiller();
                    fBiller= (FBiller) serializable;
                    recored_fBiller_editText.setText(fBiller.getFName());
//                    setFocusOnNonValueComponent();
                }
                break;
        }
        return  true;
    }

    /**
     * 设置Bundle数据源
     * @param searchType
     */
    private void setBundle(int searchType){
        sendBundle = new Bundle();
        switch (searchType){
            case SearchType.SEARCH_FORG_TYPE:
                if (null!=fOrg){
                    sendBundle.putSerializable("RecoredFOrg",fOrg);
                }
                sendBundle.putInt("SearchType", SearchType.SEARCH_FORG_TYPE);
                this.searchType =SearchType.SEARCH_FORG_TYPE;//缓存搜索文本框类型
                break;
            case SearchType.SEARCH_FSELBILLNO_TYPE:
                if (null!=fOrg){
                    sendBundle.putSerializable("RecoredFSelBillNoFOrg",fOrg);
                }
                sendBundle.putInt("SearchType", SearchType.SEARCH_FSELBILLNO_TYPE);
                this.searchType =SearchType.SEARCH_FSELBILLNO_TYPE;//缓存搜索文本框类型
                break;
            case SearchType.SEARCH_FDEPT_TYPE:
                if (null!=fDept){
                    sendBundle.putSerializable("RecoredFDept",fDept);
                }
                sendBundle.putInt("SearchType", SearchType.SEARCH_FDEPT_TYPE);
                this.searchType =SearchType.SEARCH_FDEPT_TYPE;//缓存搜索文本框类型
                break;
            case SearchType.SEARCH_FEMP_TYPE:
                if (null!=fEmp){
                    sendBundle.putSerializable("RecoredFEmp",fEmp);
                }
                sendBundle.putInt("SearchType", SearchType.SEARCH_FEMP_TYPE);
                this.searchType =SearchType.SEARCH_FEMP_TYPE;//缓存搜索文本框类型
                break;
            case SearchType.SEARCH_FFMANGER_TYPE:
                if (null!=ffManger){
                    sendBundle.putSerializable("RecoredFFManger",ffManger);
                }
                sendBundle.putInt("SearchType", SearchType.SEARCH_FFMANGER_TYPE);
                this.searchType =SearchType.SEARCH_FFMANGER_TYPE;//缓存搜索文本框类型
                break;
            case SearchType.SEARCH_FBILLER_TYPE:
                if (null!=fBiller){
                    sendBundle.putSerializable("RecoredFBiller",fBiller);
                }
                sendBundle.putInt("SearchType", SearchType.SEARCH_FBILLER_TYPE);
                this.searchType =SearchType.SEARCH_FBILLER_TYPE;//缓存搜索文本框类型
                break;
        }
    }

    /**
     * 设置单据信息
     * @param docInfoDTO
     */
    private void setDocInfoDTO(DocInfoDTO docInfoDTO){
        //销售出库单
        if (docInfoDTO.getDocType()== DocType.DOC_TYPE_SSCK) {
            docInfoDTO.setFBillno(recored_fBillno_txt.getText().toString());
            docInfoDTO.setFOrg(this.fOrg);
            docInfoDTO.setFDept(this.fDept);
            docInfoDTO.setFEmp(this.fEmp);
            docInfoDTO.setFfManger(this.ffManger);
            docInfoDTO.setFBiller(this.fBiller);
            docInfoDTO.setSelectedList(this.seOutStockList);
        }
    }

    /**
     * 点击事件处理
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recored_commit://确认按钮
                //进行数据校验(非空判断、数据有效性判断)
                int validStatue=ValidRecoredData();
                if (0==validStatue){//存在空数据
                    Toast.makeText(getContext(), "请录入完整!", Toast.LENGTH_SHORT).show();
                    break;
                } else if (1==validStatue){//数据校验成功
                    //存储单据头信息
                    setDocInfoDTO(docInfoDTO);
                    //设置传输Bundle，表示录入正确
                    Bundle recoredStatus=new Bundle();
                    recoredStatus.putInt("RecoredStatus",200);
                    //判断了当前绑定的Activity是否实现了该接口，如果实现了 则调用
                    if (getActivity() instanceof FRecoredListener) {
                        ((FRecoredListener) getActivity()).onFRecoredCommitClick(v,recoredStatus);
                    }
                    break;
                }
//                else{//数据校验失败，存在不正确数据
//                    switch (this.validCode){
//                        case SearchType.SEARCH_FORG_TYPE:
//                            Toast.makeText(getContext(), "购货单位不存在!", Toast.LENGTH_SHORT).show();
//                            break;
//                        case SearchType.SEARCH_FSELBILLNO_TYPE:
//                            Toast.makeText(getContext(), "选单号无效!", Toast.LENGTH_SHORT).show();
//                            break;
//                        case SearchType.SEARCH_FDEPT_TYPE:
//                            Toast.makeText(getContext(), "部门不存在!", Toast.LENGTH_SHORT).show();
//                            break;
//                        case SearchType.SEARCH_FEMP_TYPE:
//                            Toast.makeText(getContext(), "业务员不存在!", Toast.LENGTH_SHORT).show();
//                            break;
//                        case SearchType.SEARCH_FFMANGER_TYPE:
//                            Toast.makeText(getContext(), "发货员不存在!", Toast.LENGTH_SHORT).show();
//                            break;
//                        case SearchType.SEARCH_FBILLER_TYPE:
//                            Toast.makeText(getContext(), "制单员不存在!", Toast.LENGTH_SHORT).show();
//                            break;
//                    }
//                    break;
//                }
            case R.id.recored_forg_editText://购货单位编辑框
                //设置Bundle数据源
                setBundle(SearchType.SEARCH_FORG_TYPE);
                //判断了当前绑定的Activity是否实现了该接口，如果实现了 则调用
                if (getActivity() instanceof FRecoredListener) {
                    ((FRecoredListener) getActivity()).onFRecoredEditTextClick(v, sendBundle);
                }
                break;
            case R.id.recored_fSelBillNo_editText://选单号编辑框
                //需判断是否填写购货单位才能进行选单号操作
                String forgText = recored_forg_editText.getText().toString();
                if (TextUtils.isEmpty(forgText)){
                    Toast.makeText(getContext(),"请先选择购货单位！",Toast.LENGTH_SHORT).show();
                    break;
                }
                //设置Bundle数据源
                setBundle(SearchType.SEARCH_FSELBILLNO_TYPE);
                //判断了当前绑定的Activity是否实现了该接口，如果实现了 则调用
                if (getActivity() instanceof FRecoredListener) {
                    ((FRecoredListener) getActivity()).onFRecoredEditTextClick(v, sendBundle);
                }
                break;
            case R.id.recored_fDept_editText://部门编辑框
                //设置Bundle数据源
                setBundle(SearchType.SEARCH_FDEPT_TYPE);
                //判断了当前绑定的Activity是否实现了该接口，如果实现了 则调用
                if (getActivity() instanceof FRecoredListener) {
                    ((FRecoredListener) getActivity()).onFRecoredEditTextClick(v, sendBundle);
                }
                break;
            case R.id.recored_fEmp_editText://业务员编辑框
                //设置Bundle数据源
                setBundle(SearchType.SEARCH_FEMP_TYPE);
                //判断了当前绑定的Activity是否实现了该接口，如果实现了 则调用
                if (getActivity() instanceof FRecoredListener) {
                    ((FRecoredListener) getActivity()).onFRecoredEditTextClick(v, sendBundle);
                }
                break;
            case R.id.recored_fFManager_editText://发货员编辑框
                setBundle(SearchType.SEARCH_FFMANGER_TYPE);
                //判断了当前绑定的Activity是否实现了该接口，如果实现了 则调用
                if (getActivity() instanceof FRecoredListener) {
                    ((FRecoredListener) getActivity()).onFRecoredEditTextClick(v, sendBundle);
                }
                break;
            case R.id.recored_fBiller_editText://制单员编辑框
                setBundle(SearchType.SEARCH_FBILLER_TYPE);
                //判断了当前绑定的Activity是否实现了该接口，如果实现了 则调用
                if (getActivity() instanceof FRecoredListener) {
                    ((FRecoredListener) getActivity()).onFRecoredEditTextClick(v, sendBundle);
                }
                break;
        }
    }

    /**
     * 缓存数据至ViewModel
     */
    private void setRecoredViewModel(){
        if (null!= recored_forg_editText){
            String s = recored_forg_editText.getText().toString();
            recoredViewModel.setfSupplyText(s);
        }
        if (null!=recored_fSelBillNo_editText){
            String s = recored_fSelBillNo_editText.getText().toString();
            recoredViewModel.setfSelBillNoText(s);
        }
        if (null!=recored_fDept_editText){
            String s = recored_fDept_editText.getText().toString();
            recoredViewModel.setfDeptText(s);
        }
        if (null!=recored_fEmp_editText){
            String s = recored_fEmp_editText.getText().toString();
            recoredViewModel.setfEmpText(s);
        }
        if (null!=recored_fFManager_editText){
            String s = recored_fFManager_editText.getText().toString();
            recoredViewModel.setfFManagerText(s);
        }
        if (null!=recored_fBiller_editText){
            String s = recored_fBiller_editText.getText().toString();
            recoredViewModel.setfBillerText(s);
        }
        if (0!= searchType){
            recoredViewModel.setSearchType(searchType);//缓存搜索类型
        }
        if (null!=fOrg){
            recoredViewModel.setfOrgMLD(fOrg);
        }
        if (null!=fDept){
            recoredViewModel.setfDeptMLD(fDept);
        }
        if (null!=ffManger){
            recoredViewModel.setFfMangerMLD(ffManger);
        }
        if (null!=fEmp){
            recoredViewModel.setfEmpMLD(fEmp);
        }
        if (null!=fBiller){
            recoredViewModel.setFBillerMLD(fBiller);
        }
        if (null!=seOutStockList){
            recoredViewModel.setSeOutStockListMLD(seOutStockList);
        }
    }

    /**
     * 缓存数据至ViewModel中
     */
    @Override
    public void onPause() {
        super.onPause();
        setRecoredViewModel();
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();//解绑butterknife视图
    }
}